package com.synjones.handsetS8.ild.pages.select;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.synjones.handsetS8.R;;
import com.synjones.handsetS8.ild.adapter.SelectItemRvAdapter;
import com.synjones.handsetS8.ild.base.BaseRecyclerAdapter;
import com.synjones.handsetS8.ild.net.api.WorkerRegApi;
import com.synjones.handsetS8.ild.net.common.NetCallback;
import com.synjones.handsetS8.ild.net.common.NetResponse;
import com.synjones.handsetS8.ild.net.model.LaborResult;
import com.synjones.handsetS8.ild.net.model.NetResult;
import com.synjones.handsetS8.ild.net.model.TeamResult;
import com.synjones.handsetS8.ild.net.model.WorkTypeRedundantResult;
import com.synjones.handsetS8.ild.net.model.WorkTypeResult;

import java.util.ArrayList;
import java.util.List;

public class SelectActivity extends AppCompatActivity implements View.OnClickListener, BaseRecyclerAdapter.OnItemClickListener<IdNameItem> {
    private ImageView ivBack;
    private TextView tvTitle;
    private RecyclerView rvItemList;

    private SelectItemRvAdapter adapter;
    private List<IdNameItem> itemList = new ArrayList<>();
    //数据类型 0 分包单位 1 班组 2 工种
    private int dataType = 0;
    //当数据类型为班组时，需要匹配分包公司id
    private long laborId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_select);
        initView();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        ivBack = (ImageView) findViewById(R.id.iv_back);
        tvTitle = (TextView) findViewById(R.id.tv_title);
        rvItemList = (RecyclerView) findViewById(R.id.rv_item_list);

        ivBack.setOnClickListener(this);
        tvTitle.setText("身份证读取");

        //获取传递的参数
        getExtras();

        //渲染到列表
        adapter = new SelectItemRvAdapter(this, itemList);
        adapter.setOnItemClickListener(this);
        rvItemList.setLayoutManager(new LinearLayoutManager(this));
        rvItemList.setAdapter(adapter);

        //获取数据
        getData();
    }

    /**
     * 获取数据
     */
    private void getData() {
        //判断数据类型
        switch (dataType) {
            //分包单位
            case 0:
                WorkerRegApi.getLaborList(new NetCallback<NetResult<List<LaborResult>>>() {
                    @Override
                    protected void onComplete(NetResponse<NetResult<List<LaborResult>>> netResponse) {
                        if (netResponse.isSuccess) {
                            List<LaborResult> laborResultList = netResponse.data.getData();
                            for (LaborResult laborsResult : laborResultList) {
                                itemList.add(new IdNameItem(laborsResult.getId(), laborsResult.getName()));
                            }
                            adapter.updateData(itemList);
                        }
                    }
                });
                break;
            //班组
            case 1:
                WorkerRegApi.getTeamList(laborId, new NetCallback<NetResult<List<TeamResult>>>() {
                    @Override
                    protected void onComplete(NetResponse<NetResult<List<TeamResult>>> netResponse) {
                        if (netResponse.isSuccess) {
                            System.out.println(new Gson().toJson(netResponse.data));
                            List<TeamResult> teamResultList = netResponse.data.getData();
                            for (TeamResult teamResult : teamResultList) {
                                itemList.add(new IdNameItem(Long.parseLong(teamResult.getId()), teamResult.getName()));
                            }
                            adapter.updateData(itemList);
                        }
                    }
                });
                break;
            //工种
            case 2:
                WorkerRegApi.getWorkTypeList(new NetCallback<NetResult<WorkTypeRedundantResult>>() {
                    @Override
                    protected void onComplete(NetResponse<NetResult<WorkTypeRedundantResult>> netResponse) {
                        if (netResponse.isSuccess) {
                            System.out.println(new Gson().toJson(netResponse.data));
                            List<WorkTypeResult> workTypeResultList = netResponse.data.getData().getList();
                            for (WorkTypeResult workTypeResult : workTypeResultList) {
                                long id = Long.parseLong(workTypeResult.getValue());
                                itemList.add(new IdNameItem(id, workTypeResult.getLabel()));
                            }
                            adapter.updateData(itemList);
                        }
                    }
                });
                break;
            default:
                break;
        }
    }

    /**
     * 获取传递参数
     */
    private void getExtras() {
        Intent intent = getIntent();
        //设置标题
        String title = intent.getStringExtra("title");
        if (null != title && !"".equals(title.trim())) {
            tvTitle.setText(title);
        }
        //获取数据类型
        dataType = intent.getIntExtra("data_type", 0);
        if (dataType == 1) {
            laborId = intent.getLongExtra("labor_id", -1L);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //返回
            case R.id.iv_back:
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemClick(String tag, View itemView, int position, IdNameItem item) {
        Intent intent = new Intent();
        intent.putExtra("select_item_id", item.getId());
        intent.putExtra("select_item_name", item.getName());
        setResult(RESULT_OK, intent);
        finish();

    }
}